﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Threading;

namespace ProgressBar
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        /// <summary>
        /// 代理
        /// </summary>
        /// <param name="ipos"></param>
        /// <param name="vinfo"></param>
        private delegate void SetPos(int ipos, string vinfo);

        private void SetTextMessage(int ipos, string vinfo)
        {
            if (this.InvokeRequired)
            {
                SetPos setpos = new SetPos(SetTextMessage);
                this.Invoke(setpos, new object[] {ipos, vinfo});
            }
            else
            {
                this.label1.Text = ipos.ToString() + "/1000";
                this.progressBar1.Value = Convert.ToInt32(ipos);
                this.textBox1.AppendText(vinfo);
            }
        }
        private void Button1_Click(object sender, EventArgs e)
        {
            Thread fhThread=new Thread(new ThreadStart(SleepT));
            fhThread.Start();
        }
        /// <summary>
        /// 新的线程执行函数
        /// </summary>
        private void SleepT()
        {
            for (int i = 0; i < 500; i++)
            {
                System.Threading.Thread.Sleep(10);
                SetTextMessage(100*i/500,i.ToString()+"\r\n");
            }
        }
    }
}
